package para3.Senior.client;

import para3.Senior.server.CrazyitMap;
import para3.Senior.server.CrazyitProtocol;
import para3.Senior.client.ClientThread;

import javax.swing.*;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {
    private static final int SERVER_PORT = 30000;
    private Socket socket;
    private PrintStream ps;
    private BufferedReader brServer;
    private BufferedReader keyIn;

    public void init(){
        try {
            keyIn = new BufferedReader(
                    new InputStreamReader(System.in)
            );

            socket = new Socket("127.0.0.1", SERVER_PORT);
            ps = new PrintStream(socket.getOutputStream());
            brServer = new BufferedReader(
                    new InputStreamReader(socket.getInputStream())
            );
            String tip = "";
            while (true){
                String userName = JOptionPane.showInputDialog(tip + "Please input hte username:");
                ps.println(CrazyitProtocol.USER_ROUND + userName + CrazyitProtocol.USER_ROUND);
                String result = brServer.readLine();
                if (result.equals(CrazyitProtocol.NAME_REP)){
                    tip = "Username Repeat! Please input again";
                    continue;
                }
                if (result.equals(CrazyitProtocol.LOGIN_SUCCESS)){
                    break;
                }
            }
        }
        catch (UnknownHostException ex){
            System.out.println("Cannot Find the Server, Please check the server init!");
            closeRs();
            System.exit(1);

        }
        catch (IOException ex){
            ex.printStackTrace();
        }
        new Thread(new ClientThread(brServer)).start();
    }

    private void readAndSend(){
        try{
            String line = null;
            while ((line = keyIn.readLine()) != null){
                if (line.indexOf(":") > 0 && line.startsWith("//")){
                    line = line.substring(2);
                    ps.println(CrazyitProtocol.PRIVATE_ROUND +
                    line.split(":")[0] + CrazyitProtocol.SPLIT_SIGN
                    + line.split(":")[1] + CrazyitProtocol.PRIVATE_ROUND);
                }
                else{
                    ps.println(CrazyitProtocol.MSG_ROUND + line
                    + CrazyitProtocol.MSG_ROUND);
                }
            }
        }
        catch (IOException ex){
            System.out.println("Net Error! Please Login Again!");
            closeRs();
            System.exit(1);
        }
    }

    private void closeRs(){
        try {
            if (keyIn != null){
                ps.close();
            }
            if (brServer != null){
                ps.close();
            }
            if (ps!=null){
                ps.close();
            }
            if (socket != null){
                keyIn.close();
            }
        }
        catch (IOException ex){
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        var client = new Client();
        client.init();
        client.readAndSend();
    }
}
